package org.example.controller;

import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 获取所有用户
    @GetMapping
    public List<User> getAllUsers() {
        return userService.list();
    }

    // 根据 id 获取用户
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getById(id);
    }

    // 添加用户
    @PostMapping
    public String addUser(@RequestBody User user) {
        boolean save = userService.save(user);
        return save ? "用户添加成功" : "用户添加失败";
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        boolean remove = userService.removeById(id);
        return remove ? "用户删除成功" : "用户删除失败";
    }
}
